/**
 * ConcreteTabuList.java
 * ************************** 
 * @date May 12, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package gossipServices.bootstraping.tman.tabuList;

import java.util.ArrayDeque;
import java.util.Deque;

import gossipServices.basic.nodeDescriptors.NodeDescriptor;

/**
 * Description: 
 * This class is an implementation of the TabuList.
 * It provides all the basic function required by 
 * the TabuList. 
 *
 */
public class ConcreteTabuList implements TabuList {

	protected Deque<NodeDescriptor> queue;
	protected int maxSize;
	
	// For Debug Only
	public Deque<NodeDescriptor> getQueue(){
		return queue;
	}
	
	public ConcreteTabuList(int maxSize){
		this.maxSize =maxSize;
		queue = new ArrayDeque<NodeDescriptor>(maxSize);
	}
	
	@Override
	public boolean add(NodeDescriptor node) {
		boolean ret = false;
		ret = queue.offerFirst(node);
		while(queue.size() > maxSize){
			queue.pollLast();
		}
		return ret;
	}

	@Override
	public boolean boundSize(int maxSize) {
		int actualSize = queue.size();
		int hasToPoll = actualSize - maxSize;
		this.maxSize = maxSize;
		for(int i=0; i< hasToPoll; i++){
			queue.pollLast();
		}
		return true;
	}

	@Override
	public boolean contains(NodeDescriptor node) {
		if(queue.size() > 0)
			return queue.contains(node);
		return false;
	}
	
	@Override
	public int getMaxSize() {
		return maxSize;
	}

	@Override
	public int size() {
		return queue.size();
	}
	
	public String toString(){
		String ret = new String("");
		int pos = 0;
		ret += "[";
		for(NodeDescriptor node : queue){
			ret += pos+") "+ node+", ";
			pos++;
		}
		ret += "]";
		return ret;
	}

}
